﻿Imports Autodesk.AutoCAD.DatabaseServices
Public Class CLDS_Segment_Container
    Private pCA As CLDS_ACAD
    Private pPS As Autodesk.AutoCAD.Windows.Palette
    Private pTB As Integer = 1
    Private qbeSSegID As String = ""
    Private qbeSFromID As String = ""
    Private qbeSToID As String = ""
    Private qbeSRway As String = ""
    Private qbeSTierNo As String = ""
    Private qbeSNRType As String = ""
    Private qbeSSgCode As String = ""
    Private qbeSVFTest As String = ""
    Private qbeSVFVal As String = ""
    Private qbeVessel As String = ""
    Private pVessel As Integer = 1
    Public Property CA() As CLDS_ACAD
        Get
            CA = pCA
        End Get
        Set(ByVal value As CLDS_ACAD)
            pCA = value
        End Set
    End Property
    Public Property PSParent() As Autodesk.AutoCAD.Windows.Palette
        Get
            PSParent = pPS
        End Get
        Set(ByVal value As Autodesk.AutoCAD.Windows.Palette)
            pPS = value
        End Set
    End Property
    Public Property TB() As Integer
        Get
            TB = pTB
        End Get
        Set(ByVal value As Integer)
            pTB = value
        End Set
    End Property
    Public Property Vessel() As Integer
        Get
            Return pVessel
        End Get
        Set(ByVal value As Integer)
            pVessel = value
        End Set
    End Property
    Public Sub Initialize(ByVal p_CA As CLDS_ACAD)
        Me.pCA = p_CA
        If pCA.pTokens("PD_MULTITIER") = "Y" Then
            Me.lbTiers.Visible = True
            Me.gbTiers.Visible = True
            rbTier1.Checked = True
        Else
            Me.lbTiers.Visible = False
            Me.gbTiers.Visible = False
        End If
        TB = 1
    End Sub
    Private Sub gbTiers_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles _
        rbTier1.CheckedChanged, _
        rbTier2.CheckedChanged, _
        rbTier3.CheckedChanged, _
        rbTier4.CheckedChanged, _
        rbTier5.CheckedChanged, _
        rbTier6.CheckedChanged, _
        rbTier7.CheckedChanged, _
        rbTier8.CheckedChanged, _
        rbTier9.CheckedChanged
        Dim rb As RadioButton = DirectCast(sender, RadioButton)
        If rb.Checked Then
            Dim n As String = rb.Name
            n = Mid$(n, Len(n), 1)
            pTB = Val(n)
            If Me.tbSegID.ReadOnly Then
                pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
            End If
        End If
    End Sub
    Public Sub ShowSegment()
        Dim fn As String = "", tn As String = ""
        If Me.tbSegID.Text = "" And (Me.tbFromNode.Text = "" Or Me.tbToNode.Text = "") Then
            Me.tbSegID.Text = ""
            Me.tbFromNode.Text = ""
            Me.tbToNode.Text = ""
            Me.gbSegment.Update()
            Me.pTB = 1
            pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
            Me.gbSegment.Update()
        Else
            pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
            Me.gbSegment.Update()
        End If
        Me.tbSegID.ReadOnly = True
        Me.tbFromNode.ReadOnly = True
        Me.tbToNode.ReadOnly = True
    End Sub
    Public Sub ShowVessel()
        Me.gbSegment.Text = "Segment details"
        If pCA.pCMAPI.CMUnits > 1 Then
            Me.gbSegment.Text = Me.gbSegment.Text & " [" & pCA.pCMAPI.CMUnitdesc & " " & Trim(Str$(Me.pVessel)) & "]"
        End If
        Me.gbSegment.Update()
    End Sub

    Private Sub btExeqry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExeqry.Click
        Dim fn As String = "", tn As String = ""
        Dim segid As String = Me.tbSegID.Text
        Dim qbeID As String = segid
        If qbeSTierNo = "" Then
            pTB = 1
        Else
            pTB = Val(qbeSTierNo)
        End If
        Dim qbe As Boolean = False
        If Not Me.tbSegID.ReadOnly Then
            qbe = True
            qbeSSegID = Me.tbSegID.Text
            qbeSFromID = Me.tbFromNode.Text
            qbeSToID = Me.tbToNode.Text
            qbeSRway = Me.tbRaceway.Text
            qbeSSgCode = Me.tbSeg.Text
            qbeSNRType = Me.tbType.Text
            If Me.tbFill.Text = "" Then
                qbeSVFTest = ""
                qbeSVFVal = ""
            Else
                qbeSVFVal = Replace(Trim(Me.tbFill.Text), " ", "")
                If Mid$(qbeSVFVal, 1, 1) = ">" Then
                    qbeSVFTest = ">"
                    qbeSVFVal = Mid$(qbeSVFVal, 2)
                Else
                    qbeSVFTest = "<"
                End If
                Dim l As Integer = Len(qbeSVFVal)
                For i As Integer = 1 To l
                    If Asc(Mid$(qbeSVFVal, 1, 1)) >= Asc("0") And Asc(Mid$(qbeSVFVal, 1, 1)) <= Asc("9") Then
                        Exit For
                    End If
                    qbeSVFVal = Mid$(qbeSVFVal, 2)
                    If qbeSVFVal = "" Then
                        qbeSVFTest = ""
                        Exit For
                    End If
                Next
            End If
            If pCA.pTokens("PD_MULTITIER") = "Y" Then
                qbeSTierNo = 0
                For i As Integer = 1 To 9
                    Dim rb As RadioButton = Me.gbTiers.Controls.Item("rbTier" & Trim(Str$(i)))
                    If rb.Checked Then
                        qbeSTierNo = i
                        pTB = Val(qbeSTierNo)
                        Exit For
                    End If
                Next
            Else
                qbeSTierNo = 1
                pTB = 1
            End If
            Me.tbSegID.ReadOnly = True
            Me.tbFromNode.ReadOnly = True
            Me.tbToNode.ReadOnly = True
            Me.tbRaceway.ReadOnly = True
            Me.tbSeg.ReadOnly = True
            Me.tbType.ReadOnly = True
            Me.tbFill.ReadOnly = True
            If InStr(qbeSSegID, "%") > 0 Or InStr(qbeSSegID, "_") > 0 _
            Or InStr(qbeSFromID, "%") > 0 Or InStr(qbeSFromID, "_") > 0 _
            Or InStr(qbeSToID, "%") > 0 Or InStr(qbeSToID, "_") > 0 _
            Or qbeSRway <> "" _
            Or qbeSSgCode <> "" _
            Or qbeSNRType <> "" _
            Or qbeSVFVal <> "" Then
                qbeID = ""
                If qbeSSegID <> "" Then
                    qbeID = qbeID & " SEGID=""" & qbeSSegID & """"
                End If
                If qbeSFromID <> "" Then
                    qbeID = qbeID & " FNODE=""" & qbeSFromID & """"
                End If
                If qbeSToID <> "" Then
                    qbeID = qbeID & " TNODE=""" & qbeSToID & """"
                End If
                If qbeSRway <> "" Then
                    qbeID = qbeID & " RWAY=""" & qbeSRway & """"
                End If
                If qbeSSgCode <> "" Then
                    qbeID = qbeID & " SGCODE=""" & qbeSSgCode & """"
                End If
                If qbeSNRType <> "" Then
                    qbeID = qbeID & " NRTYPE=""" & qbeSNRType & """"
                End If
                If qbeSVFVal <> "" Then
                    If qbeSVFTest = ">" Then
                        qbeID = qbeID & " FPCGT=""" & qbeSVFVal & """"
                    Else
                        qbeID = qbeID & " FPCLT=""" & qbeSVFVal & """"
                    End If
                End If
                If qbeSTierNo > 0 Then
                    qbeID = qbeID & " TIER=""" & qbeSTierNo & """"
                End If
            End If
        End If
        Me.gbQBESFilter.Visible = False
        Me.gbCables.Visible = True
        Me.gbQBESFilter.Update()
        Me.gbCables.Update()
        Dim qVessel As Integer = 0
        If qbe Then
            If Me.cbQBEVessel.Text = "-" Then
                qVessel = 0
            Else
                qVessel = Val(Me.cbQBEVessel.Text)
            End If
        Else
            qVessel = Val(pCA.pProps.v_focus)
        End If
        If InStr(qbeID, "=""") = 0 And Me.tbSegID.Text = "" And (Me.tbFromNode.Text = "" Or Me.tbToNode.Text = "") Then
            Me.tbSegID.Text = ""
            Me.tbFromNode.Text = ""
            Me.tbToNode.Text = ""
            Me.gbSegment.Update()
            Me.pTB = 0
            pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
            Me.gbSegment.Update()
            pCA.CLDS_PickSegment(fn, tn)
            If Not (fn = "" Or tn = "") Then
                Me.tbFromNode.Text = fn
                Me.tbToNode.Text = tn
                Me.gbSegment.Update()
                pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB, qVessel)
            End If
        Else
            pCA.CLDS_SegmentDetail(Me, Trim(qbeID), Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB, qVessel)
            Me.gbSegment.Update()
        End If
        If Not pCA.pMultiCabDialog Is Nothing Then
            pCA.pMultiCabDialog.mrToolStripButton1.Enabled = False
            pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = ""
            pCA.pMultiCabDialog.mrToolStripButton2.Enabled = False
            pCA.pMultiCabDialog.mrToolStripButton1.ToolTipText = ""
            pCA.pMultiCabDialog.Update()
        End If
    End Sub

    Private Sub btCRHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCRHide.Click
        Me.tbSegID.Text = ""
        Me.tbFromNode.Text = ""
        Me.tbToNode.Text = ""
        Me.gbSegment.Update()
        Me.pTB = 1
        pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
    End Sub

    Private Sub lvCables_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvCables.ItemCheck
        Dim lv As ListView = DirectCast(sender, ListView)
        Dim li As Integer = e.Index
        Dim lvi As ListViewItem = lv.Items(li)
        Dim cab As String = lvi.SubItems(0).Text
        If e.CurrentValue = CheckState.Checked Then
            'MsgBox("Deselected cable " & cab, MsgBoxStyle.Information, "CMPIC")
            pCA.CLDS_RouteClear(pCA.pRouteDialog, cab)
        Else
            'MsgBox("Selected cable " & cab, MsgBoxStyle.Information, "CMPIC")
            pCA.CLDS_RouteHilite(pCA.pRouteDialog, cab, Nothing, Nothing)
        End If
    End Sub

    Private Sub btEntqry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEntqry.Click
        If Me.tbSegID.ReadOnly Then
            Me.tbSegID.ReadOnly = False
            Me.tbFromNode.ReadOnly = False
            Me.tbToNode.ReadOnly = False
            Me.tbRaceway.ReadOnly = False
            Me.tbSeg.ReadOnly = False
            Me.tbType.ReadOnly = False
            Me.tbFill.ReadOnly = False
            Me.tbSegID.Text = ""
            Me.tbFromNode.Text = ""
            Me.tbToNode.Text = ""
            Me.gbSegment.Update()
            If pCA.pTokens("PD_MULTITIER") = "Y" Then
                Me.lbTiers.Visible = True
                Me.gbTiers.Visible = True
                For t As Integer = 1 To 9
                    Dim rb As RadioButton = Me.gbTiers.Controls.Item("rbTier" & Trim$(Str$(t)))
                    Dim lb As Label = Me.gbTiers.Controls.Item("lbTier" & Trim$(Str$(t)))
                    rb.Visible = True
                    rb.Checked = False
                    lb.Visible = True
                Next
                Me.pTB = 0
            Else
                Me.lbTiers.Visible = False
                Me.gbTiers.Visible = False
                Me.pTB = 1
            End If
            If Not pCA.pHzPalette Is Nothing Then
                pCA.pHzPalette.Visible = False
                pCA.pMultiCabDialog.lvMultiSegment.Clear()
            End If
            pCA.CLDS_SegmentDetail(Me, Me.tbSegID.Text, Me.tbFromNode.Text, Me.tbToNode.Text, Me.pTB)
            Me.gbSegment.Update()
            Me.gbQBESFilter.Visible = True
            Me.gbCables.Visible = False
            Me.gbQBESFilter.Location = Me.gbCables.Location
            Me.lbQBEVessel.Text = pCA.pTokens("PD_UNITDESC")
            If pCA.pCMAPI.CMUnits = 1 Then
                Me.lbQBEVessel.Hide()
                Me.cbQBEVessel.Hide()
                Me.gbQBESFilter.Hide()
            Else
                Me.cbQBEVessel.Items.Clear()
                Me.cbQBEVessel.Items.Add("-")
                For i As Integer = 1 To pCA.pCMAPI.CMUnits
                    Me.cbQBEVessel.Items.Add(Trim(Str$(i)))
                Next
                Me.lbQBEVessel.Show()
                Me.cbQBEVessel.Show()
                Me.gbQBESFilter.Show()
            End If
            Me.gbQBESFilter.Update()
            Me.gbCables.Update()
            Me.cbQBEVessel.Text = Trim(Str$(CA.pProps.v_focus))
        Else
            Me.tbSegID.Text = qbeSSegID
            Me.tbFromNode.Text = qbeSFromID
            Me.tbToNode.Text = qbeSToID
            Me.tbRaceway.Text = qbeSRway
            Me.tbSeg.Text = qbeSSgCode
            Me.tbType.Text = qbeSNRType
            If qbeSVFVal <> "" Then
                If qbeSVFTest <> "<" Then
                    qbeSVFTest = ">"
                End If
                Me.tbFill.Text = qbeSVFTest & " " & qbeSVFVal
            End If
            If qbeSTierNo > 0 Then
                Dim rb As RadioButton = Me.gbTiers.Controls.Item("rbTier" & Trim(Str$(qbeSTierNo)))
                rb.Checked = True
            End If
            Me.cbQBEVessel.Text = qbeVessel
        End If
    End Sub

    Private Sub tbSegID_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbSegID.MouseDoubleClick
        pCA.CLDS_SelectSegment(Me.tbFromNode.Text, Me.tbToNode.Text, True)
    End Sub

    Private Sub tbFromNode_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbFromNode.MouseDoubleClick
        pCA.CLDS_SelectNode(Me.tbFromNode.Text, "", True)
    End Sub

    Private Sub tbToNode_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbToNode.MouseDoubleClick
        pCA.CLDS_SelectNode(Me.tbToNode.Text, "", True)
    End Sub

End Class
